package demo2接口使用案例;

class Money implements  Cloneable{
        public double money = 19.9;

        @Override
        protected Object clone() throws CloneNotSupportedException {
                return super.clone();
        }
}

class Person implements Cloneable{
public int age;
public Money m;

        public Person(int age) {
                this.age = age;
                this.m= new Money();
        }

        @Override
        public String toString() {
                return "Person{" +
                        "age=" + age +
                        '}';
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {//编译时异常，必须时编译时处理
                Person tmp = (Person) super.clone();
                tmp.m = (Money) m.clone();
                return tmp;
        }
}

public class Test2 {

        public static void main(String[] args) throws CloneNotSupportedException {
                Person person1 = new Person(10);
                Person person2 = (Person) person1.clone();//person1这里是objcet类型，把父类赋值给person，这里发生向下转型，需要强制类型转换

                System.out.println(person1.m.money);
                System.out.println(person2.m.money);//此时现在叫浅拷贝 并没有将对象中的对象进行克隆
                System.out.println("==========================");
                person2.m.money = 99.99;
                System.out.println(person1.m.money);
                System.out.println(person2.m.money);//这里就实现了深拷贝
        }

        }
